//
//  HNSH_AddressViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/26.
//

#import "HNSH_AddressViewController.h"

//view
#import "HNSH_AddressCell.h"

//vc
#import "HNSH_AddressEditViewController.h"

@interface HNSH_AddressViewController ()

/* dataArray */
@property (nonatomic, strong) NSArray *dataArray;

/* addBtn */
@property (nonatomic, strong) UIButton *addBtn;

@end

static NSString *HNSH_AddressCellID = @"HNSH_AddressCellID";

@implementation HNSH_AddressViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"地址管理" showLeftItem:YES];
    [self addAllViews];
    
    [self configTableView];
    
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self headerRefreshing];
}

//MARK: Custom Methods
- (void)addAllViews{
    [self.view addSubview:self.addBtn];
}

//MARK: 添加新收货人
- (void)addBtnAction{
    [self editAddressWithModel:nil];
}

- (void)editAddressWithModel:(HNSH_AddressModel *)model{
    HNSH_AddressEditViewController * vc = [HNSH_AddressEditViewController new];
    vc.addressModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: 获取地址列表
- (void)refreshData:(NSInteger)page {
    [HNSH_ShopNetServer requestUserAddressListSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            NSArray * list = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_AddressModel.class json:list];
            self.dataArray = array;
            [self.tableView reloadData];
            [self endRefresh:array.count >= 10];
        }
    } Failure:^(NSError * _Nullable error) {
        [self endRefresh:YES];
    }];
}

- (void)editAddressWithType:(NSInteger)type cell:(HNSH_AddressCell *)cell{
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    HNSH_AddressModel * model = self.dataArray[indexPath.row];
    if (type == 0 || type == 2) {
        [self editAddressWithModel:model];
    } else {
        WeakSelf(weakSelf);
        [HNSH_AlertView showTitle:@"确认删除此条记录？" des:@"" buttonTitle:@"确定" buttonColor:[UIColor appGreenColor] buttonClick:^{
            [weakSelf deleteAddressWithModel:model];
        }];
    }
}

- (void)deleteAddressWithModel:(HNSH_AddressModel *)model{
    [HNSH_ShopNetServer requestUserAddressDeleteWithAddressId:model.uid success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"删除成功"];
            [self headerRefreshing];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(30, kScreenHeight - 50 - 23 - kBottomSafeHeight, kScreenWidth - 60, 50);
        [_addBtn setTitle:@"添加新收货人" forState:UIControlStateNormal];
        [_addBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _addBtn.backgroundColor = [UIColor appGreenColor];
        _addBtn.layer.cornerRadius = 25;
        _addBtn.layer.masksToBounds = YES;
        [_addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

- (void)configTableView {
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, 50 + 12 + kBottomSafeHeight, 0));
    }];
    [self.tableView registerClass:[HNSH_AddressCell class] forCellReuseIdentifier:HNSH_AddressCellID];
}

//MARK: UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 18.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_AddressCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_AddressCellID];
    cell.model = self.dataArray[indexPath.section];
    WeakSelf(weakSelf);
    cell.editAddressBlock = ^(NSInteger type, HNSH_AddressCell * _Nonnull cell) {
        [weakSelf editAddressWithType:type cell:cell];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_AddressModel * model = self.dataArray[indexPath.section];
    if (self.isChoose) {
        if (self.chooseAddressBlock) {
            self.chooseAddressBlock(model);
        }
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self editAddressWithModel:model];
    }
}

@end
